//
//  MyPointViewController.m
//  ShangChengMoBan
//
//  Created by 军 on 16/8/24.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "MyPointViewController.h"
#import "MyCollectionViewCell.h"
#import "RecodeViewController.h"
#import "DetailsViewController.h"
#import "RuleViewController.h"
#import "DefiniteViewController.h"
#import "MyPointViewController.h"

#import "JJSC_firstModel.h"
@interface MyPointViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIButton *jiFen;
@property (weak, nonatomic) IBOutlet UILabel *fenShu;
@property (weak, nonatomic) IBOutlet UIImageView *image;
- (IBAction)jiLu:(id)sender;
- (IBAction)mingXi:(id)sender;
- (IBAction)qianDao:(id)sender;

@end

@implementation MyPointViewController
{
    int _page;
    NSMutableArray * _allArr ;
   RWcollectionView * _collectionView;
    UIView * _firstView;
}
-(void)headRefresh{
    _page = 1;
    [self requestData:1];
}
-(void)footerRefresh{
    _page ++;
    [self requestData:0];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self drawNavWithTitle:@"我的积分" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
    [self requestData:1];
    //添加积分规则按钮
    UIButton * guiButton = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-100, 20, 100, 44)];
    [guiButton setTitle:@"积分规则" forState:UIControlStateNormal];
    [guiButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    guiButton.titleLabel.font=[UIFont systemFontOfSize:15];
    [guiButton addTarget:self action:@selector(guiButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:guiButton];
    
    UIView * viewHeader = [[[NSBundle mainBundle]loadNibNamed:@"HeaderTitle" owner:self options:nil] lastObject];
    viewHeader.frame = CGRectMake(0, 0, KScreenWidth, 327);
    _jiFen.layer.cornerRadius = 35;
     //创建collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 1 ;
    flowLayout.minimumLineSpacing = 1;
    _collectionView = [[RWcollectionView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-64) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor=[UIColor lightGrayColor];
    [_collectionView addSubview:viewHeader];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_image sd_setImageWithURL:[NSURL URLWithString:@"http://img67.nongjx.com/9/20151217/635859586132632928739.jpg"]];
    [self.view addSubview:_collectionView];
    [_collectionView addHeaderWithTarget:self action:@selector(headRefresh)];
    [_collectionView addFooterWithTarget:self action:@selector(footerRefresh)];
  NSString * A = [[NSUserDefaults standardUserDefaults]valueForKey:@"JFSC"];
    if ([A isEqualToString:@"0"]) {
        [self firstDeng];
    }
    [TCUtils showLoadingWithView:self.view];
}
-(void)firstDeng{
    _firstView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    _firstView.hidden = NO;
    _firstView.alpha = 1.0;
     _firstView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    [self.view addSubview:_firstView];
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64+_jiFen.bottom-4, 20, 20)];
    image.image = [UIImage imageNamed:@"手"];
    image.centerXv = self.view.centerXv;
    UILabel * qian = [[UILabel alloc]initWithFrame:CGRectMake(0, image.bottom +2, 50, 15)];
    qian.centerXv = self.view.centerXv;;
    qian.textAlignment = NSTextAlignmentCenter;
    qian.text =@"点击签到";
    qian.textColor =[UIColor whiteColor];
    qian.font =[UIFont systemFontOfSize:11];
    UIButton * zhen = [[UIButton alloc]initWithFrame:CGRectMake(0, qian.bottom+5, 60, 20)];
    zhen.centerXv = self.view.centerXv;;
    zhen.backgroundColor = [UIColor redColor];
    [zhen setTitle: @"朕知道了" forState:UIControlStateNormal];
    zhen.titleLabel.font = [UIFont systemFontOfSize:12];
    [zhen addTarget:self action:@selector(zhen) forControlEvents:UIControlEventTouchUpInside];
    [_firstView addSubview:image];
    [_firstView addSubview:qian];
    [_firstView addSubview:zhen];
    
}
-(void)zhen{
[UIView animateWithDuration:0.4 animations:^{
    _firstView.alpha = 0.0;
}];
}
-(void)requestData:(int)first{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setObject:@"10" forKey:@"pageSize"];
    [requestDic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
    [requestDic setObject:[self defaultLoadUserName] forKey:@"userName"];
    [NetWorking NEWjieKouQingQiu:JJSC_findByExchangeGoods ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
        [TCUtils hiddonLoadingWithView:self.view];
    if (!error) {
        if ([dic[Code] intValue] == 200) {
            if (first==1) {
                _allArr = [[NSMutableArray alloc]init];
            }
            NSArray *arr= dic[Response][@"goods"];
            _fenShu.text = [NSString stringWithFormat:@"%@",dic[Response][@"user"][@"payPoints"]];
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JJSC_firstModel * allModer = [JJSC_firstModel objectFromDictionary:obj];
                [_allArr addObject:allModer];
            }];
            [_collectionView reloadData];
        }
    }
}];
    [_collectionView headerEndRefreshing];
    [_collectionView footerEndRefreshing];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
       return CGSizeMake(KScreenWidth, 327);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
   
return  CGSizeMake(KScreenWidth/2-0.5, KScreenWidth/2-0.5);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _allArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    JJSC_firstModel * moder = _allArr[indexPath.row];
    MyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:moder.goods.goodsThumb]];
    cell.title.text =moder.goods.goodsName;
    cell.num.text = [NSString stringWithFormat:@"%@",moder.exchangeIntegral];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
//进入详情页
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    JJSC_firstModel * firstModel = _allArr[indexPath.row];
    DetailsViewController * details = [[DetailsViewController alloc]init];
    details.firstMdoel=firstModel;
    [self.navigationController pushViewController:details animated:YES];
 }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//积分规则
-(void)guiButton{
    
    RuleViewController * cecode = [[RuleViewController alloc]init];
    [self.navigationController pushViewController:cecode animated:YES];
}
//记录详情
- (IBAction)jiLu:(id)sender {
                  RecodeViewController * cecode = [[RecodeViewController alloc]init];
                  [self.navigationController pushViewController:cecode animated:YES];
 }
//兑换明细
- (IBAction)mingXi:(id)sender {
                  DefiniteViewController  * defin = [[DefiniteViewController alloc]init];
                [self.navigationController pushViewController:defin animated:YES];
 }
//点击签到
- (IBAction)qianDao:(id)sender {
    [self firstDeng];
    [UIView animateWithDuration:0.4 animations:^{
        _firstView.hidden = NO;
        _firstView.alpha = 1.0;
    }];
}
@end
